home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Light ROM 2
/
LIGHT-ROM 2 (Amiga Library Services)(1995).iso
/
programs
/
pc
/
l_parser
/
lwlo.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-03-23
|
5KB
|
207 lines
/*
LWLO.C or LWOB.C
Copyright (C) 1994 Earl C. Terwilliger
158 Eades Drive
Irvine, KY 40336
Phone (606)-723-5718
Internet: aisterwi@acs.eku.edu
*/
#define BUFSIZE 4096
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <process.h>
#include <stdlib.h>
static unsigned char buf[BUFSIZE];
static unsigned char csize_buf[4];
unsigned long *csize = (long *)csize_buf;
static char debug = 0;
main(argc,argv)
int argc;
char *argv[];
{
int fp,c,d;
unsigned char form[5], type[5], chunk[5], temp[80], infile[64];
unsigned int count,length;
long bread = 0;
unsigned long points = 0;
unsigned long tpoints = 0;
float *x, *y, *z;
if (argc < 2) syntax(argv[0]);
strcpy(infile,argv[1]);
if (!strchr(argv[1], '.')) strcat(infile, ".OBJ");
fp = open(infile,O_BINARY|O_RDONLY);
if (fp == -1) {
printf("Unable to open file %s\n",infile);
syntax(argv[0]);
}
if (argc > 2) {
if ((argv[2][0] == '-') && (toupper(argv[2][1]) == 'D')) debug = 1;
}
read(fp,buf,12);
bread += 12L;
get_str(buf,form,4);
get_str(&buf[8],type,4);
rev_long(&buf[4],csize_buf);
if ((c_string(form,"FORM") != 1) || (c_string(type,"LW") != 1)) {
close(fp);
printf("%s is not a LWLO or LWOB file\n",infile);
exit(1);
}
printf("FORM = %s\n",form);
printf("TYPE = %s Length = %ld\n",type,*csize);
while (count = read(fp,buf,8)) {
bread += (long)count;
get_str(buf,chunk,4);
rev_long(&buf[4],csize_buf);
if (c_string(chunk,"PNTS")) {
points = *csize / 12l;
tpoints = 0l;
printf("Chunk=%s Size=%4ld bytes Points=%ld\n",chunk,*csize,points);
while(*csize!=0L) {
count = read(fp,buf,12);
bread += (long)count;
*csize -= 12l;
tpoints += 1l;
if (debug) {
phex(buf,12);
rev_long(&buf[0],&temp[0]);
x = (float *)&temp[0];
rev_long(&buf[4],&temp[4]);
y = (float *)&temp[4];
rev_long(&buf[8],&temp[8]);
z = (float *)&temp[8];
printf("%03ld x=%12.6f y=%12.6f z=%12.6f\n",tpoints,*x,*y,*z);
}
}
if (tpoints != points) {
printf("Calculated # of points != points read from file!\n");
break;
}
}
else {
printf("Chunk=%s Size=%4ld bytes\n",chunk,*csize);
if (*csize > (unsigned long)BUFSIZE) {
while (*csize!=0L) {
if (*csize > (long)BUFSIZE) {
count = read(fp,buf,BUFSIZE);
*csize -= (long)BUFSIZE;
}
else { count = read(fp,buf,(int)*csize); *csize = 0L; }
bread += (long)count;
}
}
else {
count = read(fp,buf,(int)*csize);
bread += (long)count;
if (c_string(chunk,"SRFS")) {
c = 0;
while (c<count) {
printf(" Surface name %s\n",buf+c);
d = strlen(buf+c);
if (d%2) c += d + 1;
else c += d + 2;
}
}
else if (c_string(chunk,"SURF")) {
c = 0;
printf(" Surface name %s\n",buf);
d = strlen(buf);
if (d%2) c += d + 1;
else c += d + 2;
sub_chunk(buf+c,count-c);
}
else if (c_string(chunk,"LAYR")) {
c = 0;
rev_int(&buf[0]);
printf(" Layer %2d Name=%s\n",
*(unsigned int *)buf,buf+4);
}
}
}
}
printf("%4ld bytes read\n",bread);
close(fp);
exit(0);
}
sub_chunk(ptr,lnth)
unsigned char *ptr;
unsigned int lnth;
{
unsigned char *iptr = ptr + 4;
unsigned int c;
char subchunk[5];
get_str(ptr,subchunk,4);
rev_int(iptr);
c = *(unsigned int *)iptr;
c += 6;
lnth -= c;
printf(" Sub Chunk %s Length %u\n",subchunk,c);
if (lnth > 0) sub_chunk(ptr+c,lnth);
return(0);
}
syntax(ptr)
char *ptr;
{
printf("%s filename[.OBJ] [-d]\n",ptr);
exit(1);
}
c_string(str1,str2)
unsigned char *str1;
unsigned char *str2;
{
while (1) {
if ((*str1 == 0) || (*str2 == 0)) return(1);
if (*str1 != *str2) return(0);
++str1;
++str2;
}
}
get_str(where,to,lnth)
unsigned char *where;
unsigned char *to;
int lnth;
{
while(lnth) {
*to = *where;
++to;
++where;
if (*where == 0) break;
--lnth;
}
*to = 0;
return(lnth);
}
phex(str,len)
unsigned char *str;
int len;
{
int c = 1;
while(len) {
if (!(c%4)) printf("%02X ",*str); else printf("%02X",*str);
++str;
--len;
++c;
}
printf("\n");
return(0);
}
rev_long(buf1,buf2)
unsigned char *buf1, *buf2;
{
buf2[0] = buf1[3];
buf2[1] = buf1[2];
buf2[2] = buf1[1];
buf2[3] = buf1[0];
return(0);
}
rev_int(ptr)
unsigned char *ptr;
{
unsigned char s;
s = ptr[0];
ptr[0] = ptr[1];
ptr[1] = s;
return(0);
}